package com.samphanie.auiu.biz_home.fragment.home

import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.samphanie.auiu.common.route.AuiuRoute
import com.samphanie.auiu.common.ui.view.loadUrl
import com.samphanie.auiu.library.utils.AuiuDisplayUtil
import com.samphanie.auiu.biz_home.model.HomeBanner
import com.samphanie.auiu.ui.banner.AuiuBanner
import com.samphanie.auiu.ui.banner.core.AuiuBannerMo
import com.samphanie.auiu.ui.item.AuiuDataItem
import com.samphanie.auiu.ui.item.AuiuViewHolder

/**
 * @author ZSY
 */
class BannerItem(val list: List<HomeBanner>) : AuiuDataItem<List<HomeBanner>, AuiuViewHolder>(list) {
    override fun onBindData(holder: AuiuViewHolder, position: Int) {
        val context = holder.itemView.context
        val banner = holder.itemView as AuiuBanner
        val models = mutableListOf<AuiuBannerMo>()
        list.forEachIndexed { index, homeBanner ->
            val bannerMo = object : AuiuBannerMo() {}
            bannerMo.url = homeBanner.cover
            models.add(bannerMo)
        }
        banner.setOnBannerClickListener { viewHolder, bannerMo, position ->
//            val intent = Intent(Intent.ACTION_VIEW, Uri.parse(list[position].url))
//            context.startActivity(intent)
            val homeBanner = list[position]
            if (homeBanner.type == HomeBanner.TYPE_GOODS) {
                //跳详情页....
                Toast.makeText(context, "you  touch me:Banner-$position", Toast.LENGTH_SHORT).show()
            } else {
                AuiuRoute.startActivity4Browser(homeBanner.url)
            }
        }
        banner.setBannerData(models)
        banner.setBindAdapter { viewHolder, mo, position ->
            ((viewHolder.rootView) as ImageView).loadUrl(mo.url)
        }
    }

    override fun getItemView(parent: ViewGroup): View? {
        val context = parent.context
        val banner = AuiuBanner(context)
        val params = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, AuiuDisplayUtil.dp2px(160f))
        // params.topMargin = AuiuDisplayUtil.dp2px(5f)
        params.bottomMargin = AuiuDisplayUtil.dp2px(10f)
        banner.layoutParams = params
        banner.setBackgroundColor(Color.WHITE)
        return banner
    }
}